var reserveWord = 'all';
var displayRoleName = ['全部','系统管理员','客户管理员'];
var valueRoleName = [reserveWord,true,false];
var displayStatusName = ['全部','正常','冻结'];

var accountGrid;
var modifyWin = modifyAccountWin();

Ext.onReady(function(){
  	Ext.lib.Ajax.defaultPostHeader += '; charset=utf-8';
  	Ext.QuickTips.init();

	//新增系统管理员窗口
	var win;
	
	//var sm = new Ext.grid.CheckboxSelectionModel();
	var store = new Ext.data.Store({
		url: 'CusAdmin!queryAdminSystem.action',
		reader: new Ext.data.JsonReader({
			root:'result.result',
			id: 'user_id',
			totalProperty: 'result.totalCount'
		},[
			{name:'user_id',mapping:'user_id'},
			{name:'user_name',mapping:'user_name'},
			{name:'company_code',mapping:'company_code'},
			{name:'display_name',mapping:'display_name'},
			{name:'company_chinese_code',mapping:'company_chinese_code'},
			{name:'create_date',mapping:'create_date'},
			{name:'disabled',mapping:'disabled'}
		])
	});
	var cm = new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer(),
		//sm,
		{header: '账户名', dataIndex: 'user_name',width:150,renderer:function(value,p,record){return "<a href=# onclick=\"modifyAccount('" + record.data.user_id + "','" + value + "','" + record.data.display_name + "')\">" + value + "</a>";}},
		{header: '显示名', dataIndex: 'display_name',width:150},
		{header: '所属公司', dataIndex: 'company_chinese_code',renderer:function(value,p,record){if(value == null){return;}return "<a href=# onclick=\"companyInfo(" + record.data.company_code+ ");return false;\">" + value + "</a>"}},
		{header: '注册时间',dataIndex:'create_date',width:150,renderer:function(value,p,record){return Ext.util.Format.date(getDate(value,'yyyy-MM-ddTHH:mm:ss'),'Y-m-d H:m:s');}},
		{header: '状态',dataIndex:'disabled',width:100,renderer:function(value,p,record){if(value == 0) return displayStatusName[1]; else return displayStatusName[2]}}
	]);

	var searchPanel = new Ext.form.FormPanel({
		//labelAlign: 'top',
		frame:true,
		
		buttonAlign:'right',
		//title: '模版信息',
		id:'searchPanelForm',
		bodyStyle:'padding:5px 5px 0',
		url:'',
		items:[{
			layout:'column',
			items:[{
				columnWidth:.5,
				layout: 'form',
			    items:[{
						xtype:'textfield',
						fieldLabel:'账号名',
						id:'user_code',
						width:300
				    },{
						xtype:'textfield',
						fieldLabel:'所属公司',
						id:'company_chinese_code',
						width:300
					}]
			},{
				columnWidth:.5,
				layout: 'form',
			    items:[new Ext.form.ComboBox({
						fieldLabel:'角色',
						id:'role',
						store:new Ext.data.SimpleStore({
							fields:['key','value'],
							data:[
								[displayRoleName[0] , valueRoleName[0]],
								[displayRoleName[1] , valueRoleName[1]],
								[displayRoleName[2] , valueRoleName[2]]
							]
						}),
						displayField:'key',
						valueField:'value',
						mode:'local',  //本地数据
						triggerAction:'all', //点击下拉显示所有数据
						emptyText:'请选择...',
						readOnly:true
						//selectOnFocus:true
					}),new Ext.form.ComboBox({
						fieldLabel:'状态',
						id:'isFreeze',
						store:new Ext.data.SimpleStore({
							fields:['key','value'],
							data:[
								[displayStatusName[0] , reserveWord],
								[displayStatusName[1] , false],
								[displayStatusName[2] , true]
							]
						}),
						displayField:'key',
						valueField:'value',
						mode:'local',  //本地数据
						triggerAction:'all', //点击下拉显示所有数据
						allowBlank:true,
						forceSelection:false,
						//hiddenName:'_is_valid',
						emptyText:'请选择...',
						readOnly:true,
						selectOnFocus:true
					})]
			}]
		}],
		buttons:[{
			text:'查询',
			id:'_btnSearch',
			handler:function(){
				store.on('beforeload' , function(){
      				store.baseParams = {
      					user_code:searchPanel.findById('user_code').getValue(),
						company_chinese_code:searchPanel.findById('company_chinese_code').getValue(),
						role:searchPanel.findById('role').getValue(),
						isFreeze:searchPanel.findById('isFreeze').getValue()
						//start:0
					}
				});
				store.load();
			}
		}]
	});
	
	searchPanel.render(Ext.getBody());

	var addAction = new Ext.Action({
		text:'新增账户',
		//iconCls:'all',
		handler:function() {
			win.show();
		}
	});

	var modifyAction = new Ext.Action({
		text:'重置密码',
		//iconCls:'all',
		handler:function() {
			Ext.Msg.alert('警告','抱歉，目前尚不支持此功能，请期待下一个版本！');
		}
	});

	accountGrid = new Ext.grid.GridPanel({
		height:document.body.clientHeight-Ext.getCmp('searchPanelForm').getInnerHeight()-Ext.getCmp('searchPanelForm').getFrameHeight(),
		id:'accountGrid',
		tbar:[addAction,modifyAction],
		bbar:new Ext.PagingToolbar({
			pageSize : pageSize,
			store:store,
			displayInfo:true,
			dispalyMsg:'显示第{0}条到{1}条记录，一共{2}条',
			emptyMsg:'没有记录'
		}),
		//sm:sm,
		cm:cm,
		store:store,
		loadMask: true,
		loadMask:{msg:'正在加载数据...'},
		autoExpandColumn:3
	});
	store.reload();

	win = addSystemAdminRoleWin(accountGrid);
	accountGrid.render(Ext.getBody());
});


function modifyAccountWin(){
	var modifyAccountPanel = new Ext.form.FormPanel({
		frame:true,
		id:'modifyAccountPanelForm',
		bodyStyle:'padding:5px 5px 0',
		url:'CusAdmin!modifyAccountByAdmin.action',
		items:[new Ext.form.Hidden({
			id:'_user_id_modify'	
		}),{
			xtype:'textfield',
			fieldLabel: '账户名',
			name: '_user_code_modify',
			id:'_user_code_modify',
			anchor:'95%',
			readOnly:true,
			disabled:true,
			allowBlank:false,
			emptyText:'请输入显示用户名',
			blankText:'显示用户名不能为空'		
		},{
			xtype:'textfield',
			fieldLabel: '显示用户名',
			name: '_user_name_modify',
			id:'_user_name_modify',
			anchor:'95%',
			allowBlank:false,
			emptyText:'请输入显示用户名',
			blankText:'显示用户名不能为空'			
		}]
	});
	var win = new Ext.Window({
		buttonalign : 'right',
		closable: true,
		draggable : true,
		region : 'center',
		plain : true,
		items : [modifyAccountPanel],
		title: '修改用户信息',
		width : 450,
		buttons : [{
			text:'修改',
			id : '_btnModify',
			handler:function(){
				if (!modifyAccountPanel.findById('_user_name_modify').validate()){
					return;
				}
				modifyAccountPanel.getForm().submit({
					success: function(form, action){
						accountGrid.getStore().reload();
						win.hide();
					},
					failure: function(form, action) {
						Ext.Msg.alert('警告','由于异常原因,操作无法完成!');
					}					
				});
			}
		},{
			text:'取消',
			id:'_btnCancel2',
			handler : function(){
				win.hide();
			}
		}],
		listeners : {"beforedestroy" : function(obj) {
				return false;
			},"beforeshow":function(obj) {
				return true;
			},"beforehide":function(obj) {
				modifyAccountPanel.getForm().reset();
				return true;
			}
		}
	});
	return win
}

function companyInfo(value) {
	var win = new Ext.Window({
		title : 'update',
		width : 800,
		items : [companyOperation(value,1,null,false)],
		buttons:[{
			text:'确定',
			handler:function(){
				doupdate();
				win.destroy();
			}
		},{
			text:'取消',
			handler:function(){
				win.destroy();
			}
		}]
	});
	win.show();
}

function modifyAccount(i,v,d) {
	var panel = modifyWin.findById('modifyAccountPanelForm');
	panel.findById('_user_id_modify').setValue(i);
	panel.findById('_user_code_modify').setValue(v);
	if (d != 'null'){
		panel.findById('_user_name_modify').setValue(d);	
	}
	modifyWin.show();
}